package cn.com.dstz.utils;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class ValidUtils{
	
	public static String [] validSuffixAndName( String fileName, String regex ){
		
		if( StringUtils.isBlank ( fileName ) || StringUtils.isBlank ( regex )){
			return null;
		}
		
		int lastIndex = fileName.lastIndexOf ( File.separator );

		fileName = lastIndex == -1 ? fileName : fileName.substring ( lastIndex + 1, fileName.length ( ) );
		
		if( regex.contains ( "," )){
			regex = regex.replace ( ",", "|" );
		}
		String name = fileName;
		
		int pointIndex = fileName.lastIndexOf(".");
		fileName = pointIndex != -1 ? fileName.substring(pointIndex + 1, fileName.length()) : fileName;
    	
    	Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    		
    	Matcher matcher = pattern.matcher( fileName );
    	
    	pattern = null;
    	
    	String suffix = matcher.matches() ? matcher.group() : null;
    	
    	return new String[]{suffix, name};
		
		
	}
	
	public static boolean validate(String validStr,String regex) {
		
		if( StringUtils.isBlank( validStr )) {
			return false;
		}
		return validStr.matches(regex);
	}

	public static boolean isNum(String num) {
		
		String regex = "^\\d+$";
		
		return validate(num, regex);
	}


	public static boolean validLengthCanEmpty( String str, int maxSize ){
		if( StringUtils.isNotBlank( str )){
			return str.length( ) > maxSize;
		}
		return false;
	}
	
	public static boolean validLength( String str, int begin, int max ){
		if( StringUtils.isBlank( str ) ){
			return true;
		}
		
		int len = str.length( );
		return len < begin || len > max;
	}
}
